home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / misc / emu / NESDevTools.lha / CHR2NAM / CHR2NAM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-17  |  1.5 KB  |  72 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define CELLSIZE 16
  5.  
  6. int main(int argc,char *argv[])
  7. {
  8.     
  9.     short int dict[CELLSIZE][256];
  10.     short int tempcell[CELLSIZE];
  11.     int current,found,match,i,j;
  12.     FILE *FI,*FC,*FO;
  13.     
  14.     fprintf(stderr,"\nName Table Creator (C)1999 Chris Covell (ccovell@direct.ca)\n\n");
  15.  
  16.   if(argc>4)
  17.   {
  18.     fprintf(stderr,"Usage: %s imagefile CHRfile NAMfile\n\n",argv[0]);
  19.     return(1);
  20.   }
  21.  
  22.   if(!(FI=argc<3? stdin:fopen(argv[1],"rb")))
  23.   { fprintf(stderr,"%s: Can't open image file\n\n",argv[0]);return(1); }
  24.   if(!(FC=argc<3? stdin:fopen(argv[2],"rb")))
  25.   { fprintf(stderr,"%s: Can't open CHR file\n\n",argv[0]);return(1); }
  26.   if(!(FO=argc<3? stdout:fopen(argv[3],"wb")))
  27.   { fprintf(stderr,"%s: Can't open output file\n\n",argv[0]);return(1); }
  28.  
  29.   current=0;
  30.   found=0;
  31.   match=1;
  32.  
  33.   for(i=0; i<256; i=i+1)
  34.   for(j=0; j<CELLSIZE; j=j+1)
  35.   dict[j][i]=fgetc(FC);
  36.   fclose(FC);
  37.  
  38.   while((tempcell[0]=fgetc(FI))>=0)
  39.   {
  40.     found=0;
  41.     current=0;
  42.     
  43.     for(i=1; i<CELLSIZE; i=i+1)
  44.     tempcell[i]=fgetc(FI);
  45.     
  46.     while((found==0) && (current<256))
  47.     {
  48.         for(i=0; ((i<CELLSIZE) && (match==1)); i=i+1)
  49.         {
  50.             if(tempcell[i] != dict[i][current]) match=0;       
  51.         }
  52.         if(match==1)
  53.         {
  54.             fputc((current & 0xFF),FO);
  55.             current=0;
  56.             found=1;
  57.         }
  58.         else current=current+1;
  59.         match=1;
  60.     }
  61.     
  62.     if(found==0)
  63.     fputc(0x00,FO);
  64.     
  65.    
  66.   }
  67.   
  68.   fclose(FO);
  69.   fclose(FI);
  70.     
  71.   return(0);
  72. }